package cn.oyjg.base.interceptor;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import cn.oyjg.base.util.Constants;

public class AuthInterceptor implements HandlerInterceptor {
	private Logger log = Logger.getLogger(AuthInterceptor.class);

	public AuthInterceptor() {
		// TODO Auto-generated constructor stub
	}

	private String mappingURL;// 利用正则映射到需要拦截的路径

	public void setMappingURL(String mappingURL) {
		this.mappingURL = mappingURL;
	}	
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
			Object handler) throws Exception {
		if(!request.getRequestURI().endsWith("sys/login.do") && request.getSession().getAttribute(Constants.user)==null ){
			PrintWriter out = response.getWriter();  
            StringBuilder builder = new StringBuilder();  
            builder.append("<script type=\"text/javascript\" charset=\"UTF-8\">");  
            builder.append("alert(\"页面过期，请重新登录\");");  
            builder.append("window.top.location.href=\"");
            builder.append(request.getContextPath());
            builder.append("/sys/login.do\";</script>");  
            out.print(builder.toString());  
            out.close();  
			return false;
		}else{
			return true;
		}
	}
	
	public void afterCompletion(HttpServletRequest arg0,
			HttpServletResponse arg1, Object arg2, Exception arg3)
			throws Exception {	
		
	}
	
	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1,
			Object arg2, ModelAndView arg3) throws Exception {
		
		
	}

}
